Introduction
This is a trial to print a Prescription using three controls: PrintPreviewDialog control, PrintDocument control and PrintDialog control.
My Project has one form (Form1) and two classes (clsReport and Globals).
Form1: has three text boxes for entering the data of the patient, the patient number and visit date.
Another three text boxes for entering the medication.
Three controls for printing: PrintPreviewDialog, PrintDocument and PrintDialog.
clsReport: has procedures for printing.
Globals: for some variables.
clsReport has the following three procedures:
- public void ReadReport()
- {
- try
- {
-
-
-
- RepVisitDate = "Date" + ": " + Globals.VisitDate;
- RepSickName = "Name" + ": " + Globals.illName;
- RepSickNum = "File number" + ": " + Globals.illNum;
-
- Medicine1 = Globals.FirstMidc;
- Medicine2 = Globals.SecondMidc;
- Medicine3 = Globals.ThirdMidc;
- }
- catch
- {
- MessageBox.Show("Reading Error.");
- }
-
- private void ComputeLeftAll(Graphics g)
- {
-
-
-
- Marginleft = Globals.leftMargin;
- Marginright = Globals.rightMargin;
- RepWid = Globals.ReportWid;
-
- int lenWork = (int)g.MeasureString(dcWork, ArialB14).Width;
- int lenDocHead1 = (int)g.MeasureString(DocHead1, ArialB18).Width;
- int lenDocHead2 = (int)g.MeasureString(DocHead2, ArialB16).Width;
-
- int lenSickVisit = (int)g.MeasureString(RepVisitDate, ArialB14).Width;
- int lenSickName = (int)g.MeasureString(RepSickName, ArialB14).Width;
- int lenSickNum = (int)g.MeasureString(RepSickNum, ArialB14).Width;
-
- int lenRepTail1 = (int)g.MeasureString(RepTail1, ArialB14).Width;
-
- xWork = Marginleft + (RepWid - lenWork) / 2;
- xDocHead1 = Marginleft + (RepWid - lenDocHead1) / 2;
- xDocHead2 = Marginleft + (RepWid - lenDocHead2) / 2;
-
- xSickName = Marginleft;
- xSickNum = Marginleft + (Marginright - Marginleft - lenSickNum) / 2;
- xSickVisit = Marginright - lenSickName;
-
- xRepTail1 = Marginleft + (Marginright - Marginleft - lenRepTail1) / 2;
- }
-
- public void DrawReport(Graphics g)
- {
- int CurrentY = Globals.topMargin;
- int CurrentX = Globals.leftMargin;
-
- try
- {
-
- ArialR14 = new Font("Arial", 14, FontStyle.Regular);
- int ArialRH14 = (int)(ArialR14.GetHeight(g));
-
- Font CorsivaFont = new Font("Monotype Corsiva", 18, FontStyle.Italic);
- int CorsivaHeight = (int)(CorsivaFont.GetHeight(g));
-
- ArialB14 = new Font("Arial", 14, FontStyle.Bold);
- int ArialBH14 = (int)(ArialB14.GetHeight(g));
- ArialB16 = new Font("Arial", 16, FontStyle.Bold);
- int ArialBH16 = (int)(ArialB16.GetHeight(g));
- ArialB18 = new Font("Arial", 18, FontStyle.Bold);
- int ArialBH18 = (int)(ArialB18.GetHeight(g));
- ArialB20 = new Font("Arial", 20, FontStyle.Bold);
- int ArialBH20 = (int)(ArialB20.GetHeight(g));
-
- ComputeLeftAll(g);
-
- SolidBrush myBrush = new SolidBrush(Color.Black);
-
- CurrentY = CurrentY + ArialRH14;
-
- g.DrawString(dcWork, ArialB14, myBrush, xWork, CurrentY);
- CurrentY = CurrentY + ArialBH14;
- g.DrawString(DocHead1, ArialB18, myBrush, xDocHead1, CurrentY);
- CurrentY = CurrentY + ArialBH20;
- g.DrawString(DocHead2, ArialB16, myBrush, xDocHead2, CurrentY);
- CurrentY = CurrentY + 2 * ArialBH14;
-
- g.DrawLine(new Pen(Brushes.Black), Marginleft, CurrentY, Marginright, CurrentY);
- CurrentY = CurrentY + ArialRH14;
-
- g.DrawString(RepSickName, ArialB14, myBrush, xSickName, CurrentY);
- g.DrawString(RepSickNum, ArialB14, myBrush, xSickNum, CurrentY);
- g.DrawString(RepVisitDate, ArialB14, myBrush, xSickVisit, CurrentY);
- CurrentY = CurrentY + 2 * ArialBH14;
-
- g.DrawString(EyeHorus, CorsivaFont, myBrush, CurrentX, CurrentY);
- CurrentY = CurrentY + CorsivaHeight;
-
- CurrentY = CurrentY + ArialRH14;
- g.DrawString(Medicine1, ArialR14, myBrush, CurrentX, CurrentY);
- CurrentY = CurrentY + ArialRH14;
- g.DrawString(Medicine2, ArialR14, myBrush, CurrentX, CurrentY);
- CurrentY = CurrentY + ArialRH14;
- g.DrawString(Medicine3, ArialR14, myBrush, CurrentX, CurrentY);
-
- CurrentY = Globals.bottomMargin - 4 * ArialRH14;
-
- g.DrawLine(new Pen(Brushes.Black), Marginleft, CurrentY, Marginright, CurrentY);
-
- CurrentY = CurrentY + ArialRH14;
- g.DrawString(RepTail1, ArialB14, myBrush, xRepTail1, CurrentY);
-
- ArialB14.Dispose();
- ArialB16.Dispose();
- ArialB18.Dispose();
- ArialB20.Dispose();
- ArialR14.Dispose();
- CorsivaFont.Dispose();
- g.Dispose();
- }
- catch
- {
- string Msg = "Can't print, no Font!";
- MessageBox.Show(Msg, "Printing.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- }
- }
Using VB to print Prescription
- Public Sub ReadReport()
- Try
- ' read data from Form1
- ' you can get the data from any data file
-
- RepVisitDate = "Date" + ": " + VisitDate
- RepSickName = "Name" + ": " + illName
- RepSickNum = "File number" + ": " + illNum
-
- Medicine1 = FirstMidc
- Medicine2 = SecondMidc
- Medicine3 = ThirdMidc
- Catch
- MessageBox.Show("Reading Error.")
- End Try
- End Sub
-
- Private Sub ComputeLeftAll(ByVal g As Graphics)
- 'compute the left location of all lines for,
- 'Recipe head, Sick data and Medicine.
-
- Dim lenWork As Integer = Convert.ToInt32(g.MeasureString(dcWork, ArialB14).Width)
- Dim lenDocHead1 As Integer = Convert.ToInt32(g.MeasureString(DocHead1, ArialB18).Width)
- Dim lenDocHead2 As Integer = Convert.ToInt32(g.MeasureString(DocHead2, ArialB16).Width)
-
- Dim lenSickVisit As Integer = Convert.ToInt32(g.MeasureString(RepVisitDate, ArialB14).Width)
- Dim lenSickName As Integer = Convert.ToInt32(g.MeasureString(RepSickName, ArialB14).Width)
- Dim lenSickNum As Integer = Convert.ToInt32(g.MeasureString(RepSickNum, ArialB14).Width)
-
- Dim lenRepTail1 As Integer = Convert.ToInt32(g.MeasureString(RepTail1, ArialB14).Width)
-
- xWork = leftMargin + (ReportWid - lenWork) / 2
- xDocHead1 = leftMargin + (ReportWid - lenDocHead1) / 2
- xDocHead2 = leftMargin + (ReportWid - lenDocHead2) / 2
-
- xSickName = leftMargin
- xSickNum = leftMargin + (rightMargin - leftMargin - lenSickNum) / 2
- xSickVisit = rightMargin - lenSickName
-
- xRepTail1 = leftMargin + (rightMargin - leftMargin - lenRepTail1) / 2
- End Sub
-
- Public Sub DrawReport(ByVal g As Graphics)
- Dim CurrentY As Integer = topMargin
- Dim CurrentX As Integer = leftMargin
-
- Try
- ' for Recipe
- ArialR14 = New Font("Arial", 14, FontStyle.Regular)
- Dim ArialRH14 As Integer = Convert.ToInt32(ArialR14.GetHeight(g))
- ' for EyeHorus
- Dim CorsivaFont As Font = New Font("Monotype Corsiva", 18, FontStyle.Italic)
- Dim CorsivaHeight As Integer = Convert.ToInt32(CorsivaFont.GetHeight(g))
- ' for Name
- ArialB14 = New Font("Arial", 14, FontStyle.Bold)
- Dim ArialBH14 = Convert.ToInt32(ArialB14.GetHeight(g))
- ArialB16 = New Font("Arial", 16, FontStyle.Bold)
- Dim ArialBH16 As Integer = Convert.ToInt32(ArialB16.GetHeight(g))
- ArialB18 = New Font("Arial", 18, FontStyle.Bold)
- Dim ArialBH18 As Integer = Convert.ToInt32(ArialB18.GetHeight(g))
- ArialB20 = New Font("Arial", 20, FontStyle.Bold)
- Dim ArialBH20 As Integer = Convert.ToInt32(ArialB20.GetHeight(g))
-
- ComputeLeftAll(g)
-
- Dim myBrush As SolidBrush = New SolidBrush(Color.Black)
-
- CurrentY = CurrentY + ArialRH14
- 'Draw Report Head:
- g.DrawString(dcWork, ArialB14, myBrush, xWork, CurrentY) 'dcWorkAR
- CurrentY = CurrentY + ArialBH14
- g.DrawString(DocHead1, ArialB18, myBrush, xDocHead1, CurrentY) 'DocHeadAR1
- CurrentY = CurrentY + ArialBH20
- g.DrawString(DocHead2, ArialB16, myBrush, xDocHead2, CurrentY) 'DocHeadAR2
- CurrentY = CurrentY + 2 * ArialBH14
- ' Draw line
- g.DrawLine(New Pen(Brushes.Black), leftMargin, CurrentY, rightMargin, CurrentY)
- CurrentY = CurrentY + ArialRH14
- ' Draw Personal Data:
- g.DrawString(RepSickName, ArialB14, myBrush, xSickName, CurrentY)
- g.DrawString(RepSickNum, ArialB14, myBrush, xSickNum, CurrentY)
- g.DrawString(RepVisitDate, ArialB14, myBrush, xSickVisit, CurrentY)
- CurrentY = CurrentY + 2 * ArialBH14
- ' Draw EyeHorus:
- g.DrawString(EyeHorus, CorsivaFont, myBrush, CurrentX, CurrentY)
- CurrentY = CurrentY + CorsivaHeight
- ' Draw Recipe:
- CurrentY = CurrentY + ArialRH14
- g.DrawString(Medicine1, ArialR14, myBrush, CurrentX, CurrentY)
- CurrentY = CurrentY + ArialRH14
- g.DrawString(Medicine2, ArialR14, myBrush, CurrentX, CurrentY)
- CurrentY = CurrentY + ArialRH14
- g.DrawString(Medicine3, ArialR14, myBrush, CurrentX, CurrentY)
- ' Go to end of page:
- CurrentY = Globals.bottomMargin - 4 * ArialRH14
- ' Draw line
- g.DrawLine(New Pen(Brushes.Black), leftMargin, CurrentY, rightMargin, CurrentY)
- ' Draw Report Tail:
- CurrentY = CurrentY + ArialRH14
- g.DrawString(RepTail1, ArialB14, myBrush, xRepTail1, CurrentY) 'RepTail1
-
- ArialB14.Dispose()
- ArialB16.Dispose()
- ArialB18.Dispose()
- ArialB20.Dispose()
- ArialR14.Dispose()
- CorsivaFont.Dispose()
- g.Dispose()
- Catch
- Dim Msg As String = "Can't print, no Font!"
- MessageBox.Show(Msg, "Printing.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
- End Try
-
- End Sub
Please read the code of Form1 then run the program to see the result.
If you have any idea or if you find any problems then please tell me.